package com.example.bootasync.service.impl;

import com.example.bootasync.entity.Order;
import com.example.bootasync.entity.bo.OrderMsgBo;
import com.example.bootasync.mapper.OrderMapper;
import com.example.bootasync.service.IOrderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author JiMing Zhou
 * @since 2023-06-02
 */
@Service
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Order> implements IOrderService {

    /**
     * 存储订单
     * @param userId 用户id
     * @param prodId 商品id
     * @return 是否成功
     */
    @Override
    public Boolean subOrder(Long userId, Long prodId, LocalDateTime createTime) {
        createTime = Objects.isNull(createTime) ? LocalDateTime.now() : createTime;
        return this.save(new Order(userId, prodId, createTime));
    }

    /**
     * MQ存储bo订单信息
     * @param bo 消息bo
     */
    @Override
    public void saveOrder(OrderMsgBo bo) {
        this.subOrder(bo.getUserId(), bo.getProdId(), bo.getCreateTime());
    }
}
